package com.googlecode.jvmpart.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class ProcessId {
	public static void main(String[] args) throws Exception {
		String pid = getPid();
		System.out.println("pid: " + pid);
		System.in.read(); // block the program so that we can do some probing on
							// it
	}

	public static String getPid() {
		RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
		String name = runtime.getName(); // format: "pid@hostname"
		try {
			return name.substring(0, name.indexOf('@'));
		} catch (Exception e) {
			return null;
		}
	}
}
